﻿using System;
using System.Data;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.News;
using BO.Video;
using GUI.Extensions;
using ChannelVN.Cached;

namespace GUI.Controls.Sport
{
    public partial class Slide : System.Web.UI.UserControl
    {
        public int ZoneId { get; set; }
        public bool IsVideo { get; set; }
        public bool GetDataFromNoCacheLayer = false;
        public string BoxTitle { get; set; }
        public string ViewMoreUrl { get; set; }

        private const int Top = 6;
        private const int ImageWidth = 140;
        private const int ImageHeight = 100;

        private const string LiFormat = @"<li class=""slide""><a title=""{0}"" href=""{1}"">{3}{4}<p>{2}</p></a></li>";
        private const string LinkFormat = "<a href=\"{0}\" title=\"{1}\">{1}</a>";
        public const string IconVideo = "<i class=\"icon-video\"></i>";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsVideo)
            {
                Visible = DataBindAsBoxVideo(ZoneId);
                return;
            }
            Visible = DataBindAsBoxNews(ZoneId);
        }

        private bool DataBindAsBoxNews(int zoneId)
        {
            if (zoneId > 0)
            {
                var objCat = NewsBO.GetInstance().GetCategoryDetail(zoneId);
                if (objCat != null)
                {
                    ltrTitle.Text = string.Format(LinkFormat, objCat.Url, objCat.Name);

                    var enableHtmlCached = GetDataFromNoCacheLayer && CacheSettingExtensions.EnableCacheHtml;
                    var isCacheUpdating = enableHtmlCached && CacheController.IsCachedUpdating;

                    var dt = enableHtmlCached
                    ? NewsBO_NoCache.GetInstance().GetSportNewsByPosition(top: Top, typeId: DefinedConstants.SportNews.TypeIds.SlideTrangChu, zoneId: ZoneId, getDataFromMasterDataBase: isCacheUpdating)
                    : NewsBO.GetInstance().GetSportNewsByPosition(top: Top, typeId: DefinedConstants.SportNews.TypeIds.SlideTrangChu, zoneId: ZoneId);

                    if (dt != null && dt.Rows.Count > 0)
                    {
                        var sb = new System.Text.StringBuilder();
                        foreach (DataRow r in dt.Rows)
                        {
                            var avatar = PresentationUtils.Photo.BuildNewsAvatar(r, ImageWidth, ImageHeight);

                            sb.AppendFormat(
                                    LiFormat,
                                    r[SqlConstants.NewsPublish.EncodedTitle],
                                    r[SqlConstants.NewsPublish.Url],
                                    r[SqlConstants.NewsPublish.Title],
                                    avatar,
                                    string.Empty
                                );
                        }
                        ltrList.Text = sb.ToString();
                        return true;
                    }
                }
            }
            return false;
        }

        private bool DataBindAsBoxVideo(int zoneId)
        {
            var dt = VideoBO.GetVideoByMode(top: Top, zoneId: zoneId, mode: 1);
            if (dt != null && dt.Rows.Count > 0)
            {
                var sb = new System.Text.StringBuilder();
                foreach (DataRow r in dt.Rows)
                {
                    var avatar = PresentationUtils.Photo.BuildVideoAvatar(r, ImageWidth, ImageHeight);

                    sb.AppendFormat(
                            LiFormat,
                            r[SqlConstants.Video.Name],
                            r[SqlConstants.Video.Url],
                            r[SqlConstants.Video.Name],
                            avatar,
                            IconVideo
                        );
                }
                ltrList.Text = sb.ToString();

                if (string.IsNullOrEmpty(ViewMoreUrl))
                {
                    var objZone = VideoBO.GetVideoZoneDetail(zoneId);
                    if (objZone != null)
                    {
                        BoxTitle = BoxTitle ?? objZone.Name;
                        ViewMoreUrl = objZone.Url;
                    }
                }
                ltrTitle.Text = string.Format(LinkFormat, ViewMoreUrl, BoxTitle);

                return true;
            }
            return false;
        }


    }
}